<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item :label="$t('ingreVat.ingreNo')" prop="ingreNo">
        <el-input
          v-model="queryParams.ingreNo"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('ingreVat.ingreName')" prop="ingreName">
        <el-input
          v-model="queryParams.ingreName"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('ingreVat.vatName')" prop="vatName">
        <el-input
          v-model="queryParams.vatName"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('ingreVat.vatType')" prop="vatType">
        <el-select
          v-model="queryParams.vatType"
          placeholder=""
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="item in vatTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
          <!--             
          <el-option label="粉料" :value="1">粉料</el-option>
          <el-option label="乳液" :value="2">乳液</el-option>
          <el-option label="助剂" :value="3">助剂</el-option>
          <el-option label="预混" :value="4">预混</el-option>
          <el-option label="调漆" :value="5">调漆</el-option> -->
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">
          {{ $t("entirety.search") }}
        </el-button>
        <el-button icon="Refresh" @click="resetQuery">
          {{ $t("entirety.reset") }}
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >
          {{ $t("entirety.add") }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:post:edit']"
        >
          {{ $t("entirety.editor") }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:post:remove']"
        >
          {{ $t("entirety.delete") }}
        </el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:post:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table
      v-loading="loading"
      border
      stripe
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column
        :label="$t('ingreVat.ingreNo')"
        align="center"
        prop="ingreNo"
      />
      <el-table-column
        :label="$t('ingreVat.ingreName')"
        align="center"
        prop="ingreName"
      />
      <el-table-column
        :label="$t('ingreVat.vatName')"
        align="center"
        prop="vatName"
      />
      <el-table-column
        :label="$t('ingreVat.vatType')"
        align="center"
        prop="vatType"
      >
        <template #default="scope">
          <div v-if="scope.row.vatType == 1">粉料</div>
          <div v-if="scope.row.vatType == 2">乳液</div>
          <div v-if="scope.row.vatType == 3">助剂</div>
          <div v-if="scope.row.vatType == 4">预混</div>
          <div v-if="scope.row.vatType == 5">调漆</div>
        </template>
      </el-table-column>
      <el-table-column label="tag" align="center" prop="tag" />
      <el-table-column
        :label="$t('ingreVat.isUse')"
        align="center"
        prop="isEnable"
      >
        <template #default="scope">
          <el-tag v-if="scope.row.isEnable == 1" type="success">
            <div>  {{ $t("ingreVat.usable") }}</div>
          </el-tag>
          <el-tag v-if="scope.row.isEnable == 0" type="danger">
            <div>  {{ $t("ingreVat.disabled") }}</div>
          </el-tag>
        </template>

        <!-- <template #default="scope">
          <dict-tag>{{ scope.row.isEnable }}++</dict-tag> -->
        <!-- <dict-tag :options="sys_normal_disable" :value="scope.row.isEnable" /> -->
        <!-- </template> -->
      </el-table-column>
      <el-table-column
        :label="$t('tasksheet.createTime')"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('entirety.handle')"
        width="180"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']"
          >
            {{ $t("entirety.editor") }}
          </el-button>
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
          >
            {{ $t("entirety.delete") }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改--弹出框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form ref="postRef" :model="form" :rules="rules" label-width="150px">
        <el-form-item :label="$t('ingreVat.ingreNo')" prop="ingreNo">
          <el-input v-model="form.ingreNo" placeholder="" />
        </el-form-item>
        <el-form-item :label="$t('ingreVat.ingreName')" prop="ingreName">
          <el-input v-model="form.ingreName" placeholder="" />
        </el-form-item>
        <el-form-item :label="$t('ingreVat.vatName')" prop="vatName">
          <el-input v-model="form.vatName" placeholder="" />
        </el-form-item>
        <el-form-item :label="$t('ingreVat.vatType')" prop="vatType">
          <el-select v-model="form.vatType" placeholder="">
            <div v-for="item in vatTypeList" :key="item.value">
              <el-option :label="item.label" :value="item.value" />
            </div>
          </el-select>
          <!-- <el-select
            v-model="queryParams.vatType"
            placeholder=""
            clearable
          >
            <el-option label="粉料" :value="1">粉料</el-option>
            <el-option label="乳液" :value="2">乳液</el-option>
            <el-option label="助剂" :value="3">助剂</el-option>
            <el-option label="预混" :value="4">预混</el-option>
            <el-option label="调漆" :value="5">调漆</el-option>
          </el-select> -->
        </el-form-item>
        <el-form-item label="tag" prop="tag">
          <el-input v-model="form.tag" placeholder=" " />
        </el-form-item>
        <el-form-item :label="$t('ingreVat.isUse')" prop="isEnable">
          <el-select v-model="form.isEnable" placeholder="" clearable>
            <el-option :label="$t('ingreVat.usable')" :value="1">
              {{ $t("ingreVat.usable") }}
            </el-option>
            <el-option :label="$t('ingreVat.disabled')" :value="0">
              {{ $t("ingreVat.disabled") }}
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="IngreVat" lang="ts">
import { reactive, ref } from "vue";
import {
  listIngreVat,
  addIngreVat,
  delIngreVat,
  getIngreVat,
  updateIngreVat,
} from "@/api/IngreVat";
const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const postList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    //  ingreNo: undefined,
    //  ingreName: undefined,
    //  isEnable: undefined

    createTime: null,
    hasWght: null,
    id: null,
    ingreName: null,
    ingreNo: null,
    isEnable: null,
    tag: null,
    updateTime: null,
    vatName: null,
    vatType: null,
  },
  rules: {
    ingreNo: [{ required: true, message: "物料编号不能为空", trigger: "blur" }],
    ingreName: [
      { required: true, message: "物料名称不能为空", trigger: "blur" },
    ],
    vatName: [{ required: true, message: "储罐代号不能为空", trigger: "blur" }],
    vatType: [{ required: true, message: "储罐类型不能为空", trigger: "blur" }],
    tag: [{ required: true, message: "tag不能为空", trigger: "blur" }],
    isEnable: [
      { required: true, message: "当前储罐是否可用不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

const vatTypeList = ref([
  {
    label: "粉料",
    value: 1,
  },
  {
    label: "乳液",
    value: 2,
  },
  {
    label: "助剂",
    value: 3,
  },
  {
    label: "预混",
    value: 4,
  },
  {
    label: "调漆",
    value: 5,
  },
] as any);

/** 查询列表 */
const getList = () => {
  loading.value = true;
  listIngreVat(
    queryParams.value.pageSize,
    queryParams.value.pageNum,
    queryParams.value
  ).then((response) => {
    postList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
};

/** 取消按钮 */
const cancel = () => {
  open.value = false;
  reset();
};

/** 表单重置 */
const reset = () => {
  form.value = {
    createTime: null,
    hasWght: null,
    id: null,
    ingreName: null,
    ingreNo: null,
    isEnable: null,
    tag: null,
    updateTime: null,
    vatName: null,
    vatType: null,
  };
  proxy.resetForm("postRef");
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  open.value = true;
  title.value = "添加储罐";
  // title.value = `${$t("ingreVat.additionTank")}`;
};

/** 修改按钮操作 */
const handleUpdate = (row) => {
  reset();
  const id = row.id || ids.value;
  getIngreVat(id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改储罐";
    // title.value = $t("ingreVat.mdifiedTank");
  });
};

/** 提交按钮 */
const submitForm = () => {
  proxy.$refs["postRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateIngreVat(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addIngreVat(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
};

/** 删除按钮操作 */
const handleDelete = (row) => {
  const ingreVatId = row.id || ids.value;
  proxy.$modal
    .confirm("是否确认删除？")
    .then(function () {
      return delIngreVat(ingreVatId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
};

/** 导出按钮操作 */
// function handleExport() {
//   proxy.download(
//     "system/post/export",
//     {
//       ...queryParams.value,
//     },
//     `post_${new Date().getTime()}.xlsx`
//   );
// }

getList();
</script>
